"""
直接添加content字段到chapters表
"""
import asyncio
import pymysql
from tool.sessing import TORTOISE_ORM

async def add_field():
    config = TORTOISE_ORM['connections']['default']['credentials']
    
    conn = pymysql.connect(
        host=config['host'],
        port=int(config['port']),
        user=config['user'],
        password=config['password'],
        database=config['database'],
        charset='utf8mb4'
    )
    
    try:
        with conn.cursor() as cursor:
            # 检查字段是否存在
            cursor.execute("""
                SELECT COLUMN_NAME 
                FROM INFORMATION_SCHEMA.COLUMNS 
                WHERE TABLE_SCHEMA = %s 
                AND TABLE_NAME = 'chapters' 
                AND COLUMN_NAME = 'content'
            """, (config['database'],))
            
            if cursor.fetchone():
                print("[INFO] content字段已存在")
            else:
                # 添加content字段
                cursor.execute("""
                    ALTER TABLE `chapters` 
                    ADD COLUMN `content` LONGTEXT NULL COMMENT '章节内容' 
                    AFTER `chapter_number`
                """)
                print("[OK] 已添加content字段")
            
            conn.commit()
    finally:
        conn.close()

asyncio.run(add_field())

